<template>
    <div style="padding: 10px;">
        <!--      搜索区-->
        <div style="margin: 10px 0">
            <el-button type="primary"  @click="load(search = '待审核')">待审核</el-button>
            <el-button type="primary"  @click="load(search = '审核通过')">审核通过</el-button>
            <el-button type="primary"  @click="load(search = '审核不通过')">审核不通过</el-button>
        </div>
        <!--      表格主体-->
        <el-table :data="tableData" border stripe style="width: 100%" height="400">
            <el-table-column prop="id" label="ID" sortable/>
            //sortable 排序功能
            <el-table-column prop="name" label="名称"/>
            <el-table-column prop="author" label="作者"/>
            <el-table-column prop="createTime" label="上传时间"/>
            <el-table-column  label="内容">
                <template #default="scope">
                    <div v-if="(/\.(jpg|jpeg|png)$/.test(scope.row.content))">
                        <el-image
                                style="width: 100px; height: 100px"
                                :src="scope.row.content"
                                preview-teleported = "true"
                                :preview-src-list="[scope.row.content]">
                        </el-image>
                    </div>
                    <div v-else >{{scope.row.content}}</div>
                </template>
            </el-table-column>
            <el-table-column prop="state" label="审核状态"/>
            <el-table-column fixed="right" label="操作" width="220" v-if="user.role ===1">
                <template #default="scope">
                    <el-popconfirm title="确认通过吗？" @confirm="changeState(scope.row, '审核通过')" :disabled="scope.row.state !== '待审核'">
                        <template #reference>
                            <el-button type="text" >审核通过</el-button>
                        </template>
                    </el-popconfirm>
                    <el-popconfirm title="确认不通过吗？" @confirm="changeState(scope.row, '审核不通过')" :disabled="scope.row.state !== '待审核'">
                        <template #reference>
                            <el-button type="text" >审核不通过</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px">
            <!--          分页功能-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :currentPage="currentPage"
                    :page-sizes="[5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
            />
            <!--        操作二次确认(弹窗)-->
            <el-dialog v-model="dialogVisible" title="提示" width="30%">
                <el-form ref="form" :model="form" label-width="120px" >
                    <el-form-item label="名称">
                        <el-input v-model="form.name" style="width: 80%"/>
                    </el-form-item>
                    <el-form-item label="作者">
                        <el-input v-model="form.author" style="width: 80%"/>
                    </el-form-item>
                    <el-form-item label="上传时间">
                        <el-date-picker v-model="form.createTime" value-format="YYYY-MM-DD" type="date" style="width: 80%;" clearable></el-date-picker>
                    </el-form-item>
                    <el-form-item  label="内容">
                        <el-upload action="http://localhost:9090/files/upload" :on-success="filesUploadSuccess">
                            <el-button type="primary">点击上传</el-button>
                        </el-upload>
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="dialogVisible = false">取消</el-button>
                        <el-button type="primary" @click="save">确认</el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    import request from "../utils/request";         //引入

    export default {
        name: 'ResourceAuditing',
        components: {},
        data() {
            return {
                downloadurl:{},
                form:{},
                dialogVisible:false,    //弹窗控制变量
                search: '待审核',             //用于模糊搜索的变量
                currentPage:1,         //记录当前页
                pageSize:10,
                total:0,
                tableData: []
            }
        },
        created() {
            let userStr = sessionStorage.getItem("user") || "{}"
            this.user = JSON.parse(userStr)
            request.get("/user/" + this.user.id).then(res => {
                if (res.code === '0') {
                    this.user = res.data
                }
            })
            this.load()
        },
        methods: {
            //实现数据渲染
            filesUploadSuccess(res){
                console.log(res)
                // if(res.data = value.replace())
                this.form.content = res.data
            },
            load(search){     //刷新页面获取数据
                request.get("/resourceAuditing",{
                    params:{
                        pageNum:this.currentPage,
                        pageSize:this.pageSize,
                        search:this.search,
                    }
                }).then(res => {
                    console.log(res.data)
                    this.tableData = res.data.records
                    this.total = res.data.total
                })
            },
            //实现新增方法
            add() {
                this.dialogVisible = true   //控制变量，以控制表单是否显示
                this.form = {}              //清空表单
                if (this.$refs['upload']) {
                    this.$refs['upload'].clearFiles()  //清空历史文件列表
                }
            },
            save(){
                if (this.$refs['upload']) {
                    this.$refs['upload'].clearFiles()  //清空历史文件列表
                }
                if (this.form.id){//更新
                    request.put("/resourceAuditing",this.form).then(res => { //请求，并传参数。 then是Es6语法用于获取后台返回结果。
                        console.log(res)            //浏览器打印res
                        if (res.code === '0'){       //判断是否成功
                            this.$message({//输出提示，用element提供的方法$message
                                type: "success",
                                message: "审核完成！"
                            })
                        } else {
                            this.$message({
                                type: "error",
                                message: res.msg
                            })
                        }
                        this.load() //更新表格数据   通过重新获取数据的方式
                        this.dialogVisible = false  //关闭弹窗
                    })
                }
                else {//新增
                    request.post("/resourceAuditing",this.form).then(res => {
                        console.log(res)
                        if (res.code === '0'){
                            this.$message({
                                type: "success",
                                message: "新增成功啦"
                            })
                        } else {
                            this.$message({
                                type: "error",
                                message: res.msg
                            })
                        }
                        this.load() //更新表格数据
                        this.dialogVisible = false  //关闭弹窗
                    })
                }
            },
            // handleEdit(row) {
            //     this.form = JSON.parse(JSON.stringify(row))
            //     this.dialogVisible = true
            //     this.$nextTick(() => {
            //         if (this.$refs['upload']) {
            //             this.$refs['upload'].clearFiles()  //清空历史文件列表
            //         }
            //     })
            // },
            changeState(row, state) {
                this.form = JSON.parse(JSON.stringify(row))
                this.form.state = state;
                this.save();
            },
            handleSizeChange(pageSize) {//改变当前每页的个数触发
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {//改变当前页码出发
                this.currentPage = pageNum
                this.load()
            },
        }
    }
</script>
